//
//  BSDetailViewController.m
//  HonlivApp
//
//  Created by admin on 2017/6/24.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "BSDetailViewController.h"
#import "UIBarButtonItem+Extension.h"

@interface BSDetailViewController ()

@property (nonatomic,strong) UIBarButtonItem *closeBtn;
@property (nonatomic,strong) UIBarButtonItem *back;
@end

@implementation BSDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)createBackBtn{
    //返回按钮
    _back = [UIBarButtonItem initWithTitle:@"返回" fontSize:16 target:self action:@selector(webViewGoBack) isBack:YES];
    
    _closeBtn = [UIBarButtonItem initWithTitle:@"关闭" fontSize:16 target:self action:@selector(closeWebView) isBack:NO];
    
    self.navigationItem.leftBarButtonItems =@[_back,_closeBtn];
}
-(void)webViewGoBack{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)closeWebView{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [self initWebView];
    [self getRequestURL];
}
-(void)viewWillDisappear:(BOOL)animated{
    
    [_webView removeFromSuperview];
}
-(void)getRequestURL{
    
}
//加载详情页
-(void)loadRequestURL:(void (^)(GetURL))doGet{
    GetURL handler = ^(NSString *url){
//        [self initWebView];
        NSURL *detailURL = [NSURL URLWithString:url];
        NSURLRequest *request = [NSURLRequest requestWithURL:detailURL];
        [_webView loadRequest:request];
    };
    doGet(handler);
}
-(void)initWebView{
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-64)];
    _webView.backgroundColor = Main_BGCOLOR;
    _webView.scrollView.bounces = NO;
    _webView.navigationDelegate = self;
    [self.view addSubview:_webView];
}

//前进、后退
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
        [self createBackBtn];
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    [SVProgressHUD show];
}
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [SVProgressHUD dismiss];
}

-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    [SVProgressHUD showErrorWithStatus:@"加载失败..."];
}

-(BOOL)navigationShouldPopOnBackButton{
    if ([self.webView canGoBack]){
        [self.webView goBack];
        return NO;
    }
    return YES;
}

#pragma mark -- JS 弹框

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler();
    }])];
    [self presentViewController:alertController animated:YES completion:nil];
    
}
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
    //    DLOG(@"msg = %@ frmae = %@",message,frame);
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:([UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(NO);
    }])];
    [alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(YES);
    }])];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:prompt message:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.text = defaultText;
    }];
    [alertController addAction:([UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(alertController.textFields[0].text?:@"");
    }])];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
